import { QueryBody } from '@api/model/queryBody.model';
import { _HttpClient } from '@delon/theme';
import { Observable } from 'rxjs';

export class Service<T, ID> {
  listUrl = '';
  constructor(public http: _HttpClient, public baseUrl: string) {
    this.listUrl = `${this.baseUrl}/findByQueryParams`;
  }

  findById(id: ID): Observable<T> {
    return this.http.get(`${this.baseUrl}/${id}`);
  }

  findByIds(ids: ID[]): Observable<T[]> {
    return this.http.post(`${this.baseUrl}/list`, ids);
  }

  findAll(): Observable<T[]> {
    return this.http.get(`${this.baseUrl}/list`);
  }

  save(t: T): Observable<T> {
    return this.http.put(`${this.baseUrl}`, t);
  }

  saveAll(ts: T[]): Observable<T> {
    return this.http.put(`${this.baseUrl}/saveAll`, ts);
  }

  deleteById(id: ID): Observable<number> {
    return this.http.delete(`${this.baseUrl}/${id}`);
  }

  deleteByIds(ids: ID[]): Observable<number> {
    const options = {
      headers: {
        'Content-Type': 'application/json',
      },
      body: ids,
    };
    return this.http.delete(`${this.baseUrl}/list`, {}, options);
  }

  isUnique(t: T): Observable<boolean> {
    return this.http.post(`${this.baseUrl}/isUnique`, t);
  }

  findByQueryParams(params: any): Observable<QueryBody<T>> {
    return this.http.get(this.listUrl, params);
  }
}
